//
//  WH_LuckViewController.m
//  App_6.23
//
//  Created by wanghao on 16/7/20.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_LuckViewController.h"
#import "AppDelegate.h"
#import "WH_Home.h"
#import "WH_OrderDetailVC.h"
#import "WH_LuckCell.h"
#import "WH_OrderSViewController.h"
#import "WH_NewDetailViewController.h"

@interface WH_LuckViewController ()<UITableViewDataSource,UITableViewDelegate,WH_LuckCellDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger Datapage;
@property (nonatomic, copy) NSString *body;

@end

@implementation WH_LuckViewController

-(void)viewWillAppear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"幸运记录";
    self.Datapage = 1;
    self.dataArr = [NSMutableArray array];
    [self createEmptylist];
    [self createMyTable];
    [self postData];
}
- (void)createMyTable{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = 170;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
    //自定义cell Xib注册
    [self.myTableView registerNib:[UINib nibWithNibName:@"WH_LuckCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"luck"];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMJ)];
    self.myTableView.mj_footer = footer;
    
    [self.myTableView.mj_footer setHidden:YES];

}
#pragma mark - 刷新/加载

-(void)upLoadMJ
{
    NSLog(@"上拉加载");
    self.Datapage++;
    [self postData];
}
#pragma mark - 结束刷新

-(void)endFresh{
    
    [self.myTableView.mj_footer endRefreshing];
    [self.myTableView.mj_header endRefreshing];
}

-(void)postData{
    
    if (_luckID.length == 0) {
        //本机用户
        _body = [NSString stringWithFormat:@"action=order_win&user_id=%@&page=%ld",NSUSERID, _Datapage];

    }else{
        //个人中心
        _body = [NSString stringWithFormat:@"action=order_win&user_id=%@&page=%ld",_luckID, _Datapage];
        
    }
    
    [WH_AppTools postUrl:URLstr body:_body result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        NSLog(@"%@",result);
        NSDictionary *dic = result;
        NSArray *order_array = [dic objectForKey:@"order_array"];
        NSInteger order_quantity = [[dic objectForKey:@"order_quantity"] integerValue];
        //NSLog(@"order_quantity==%ld",order_quantity);
        for (NSDictionary *dic in order_array) {
            
            WH_Home *luck = [WH_Home modelWithDictionary:dic];
            [self.dataArr addObject:luck];
            
        }
        //加载没数据
        //加载没有数据
        if (order_quantity == 0) {
            
            self.myTableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else{
            
            
            [self endFresh];
        }
        //没数据
        if (self.dataArr.count == 0) {
            
            [self bringSubView];
            
        }else{
            
            [self.view bringSubviewToFront:self.myTableView];
        }

        [self.myTableView.mj_footer setHidden:NO];
        [self.myTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}

- (void)imBtnAction{
    
    //UIApplication是系统单例,可以直接使用
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
    appDelegate.tabbarMain.selectedIndex = 0;
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *luck = @"luck";
    WH_LuckCell *cell = [tableView dequeueReusableCellWithIdentifier:luck];
    cell.luckOrder = self.dataArr[indexPath.row];
    cell.delegate = self;
    if (_luckID.length != 0) {
        
        [cell.shaiBtn setHidden:YES];
    }else{
        
        [cell.shaiBtn setHidden:NO];

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
}
#pragma mark - shaidan

-(void)pushToperoid:(WH_Home *)peroid{
    
    WH_OrderSViewController *messageVC = [[WH_OrderSViewController alloc] init];
    messageVC.peroid = peroid;
    [self.navigationController pushViewController:messageVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
